package socket;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

/**
 * 聊天室客户端
 */
public class Client {
    /*
        java.net.Socket
        Socket封装了TCP协议的通讯细节，使用它就可以和远端计算机建立TCP连接，并基于
        两条流(一个输入一个输出流)与远端计算机交互数据。
     */
    private Socket socket;

    public Client() {
        try {
            /*
                实例化Socket就是与远端计算机建立连接的过程。
                这里参数1:服务端计算机的IP地址，参数2:服务端打开的服务端口
                我们通过IP找到服务器计算机通过端口连接到服务器上的服务端应用程序。
             */
            System.out.println("正在连接服务端...");
            socket = new Socket("localhost",8088);
//            socket = new Socket("176.6.11.172",8088);//wy
//            socket = new Socket("176.6.11.178", 8088);//ky
//            socket = new Socket("176.6.11.177",8088);//zl
//            socket = new Socket("176.6.11.179", 8088);//zyq
            System.out.println("与服务端建立连接");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void start(int a) {
        //启动用来读取服务端消息的线程
        ServerHandler serverHandler = new ServerHandler();
        Thread t = new Thread(serverHandler);
        t.setDaemon(true);
        t.start();

        try {
            //通过socket获取输出流
            OutputStream ops = socket.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(ops, StandardCharsets.UTF_8);
            BufferedWriter bw = new BufferedWriter(osw);
            PrintWriter pw = new PrintWriter(bw, true);

            Scanner scanner = new Scanner(System.in);
            while (true) {
                String line = scanner.nextLine();
                if (line.equalsIgnoreCase("exit")) {
                    break;
                }
                pw.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        Client client = new Client();
        int a = 1;
        client.start(a);
    }

    private class ServerHandler implements Runnable{
        @Override
        public void run() {
            //通过socket获取输入流读取服务端发过来的消息
            try {
                InputStream ips = socket.getInputStream();
                InputStreamReader isr = new InputStreamReader(ips, StandardCharsets.UTF_8);
                BufferedReader br = new BufferedReader(isr);

                String message;
                while((message = br.readLine())!=null){
                    System.out.println(message);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

}










